//
//  Type_2addCViewCell.m
//  Travel_M_Travel
//
//  Created by Sun on 15/3/27.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Type_2addCViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIColor+AddColor.h"

@implementation Type_2addCViewCell

- (void)dealloc
{
    [_timeLabel release];
    [_wayLabel release];
    [_coverView release];
    [_photoView release];
    [_about2Waydata release];
    [super dealloc];
}

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
        self.photoView = [[UIImageView alloc] init];
        _photoView.clipsToBounds = YES;
        [self.contentView addSubview:_photoView];
        [_photoView release];
        
        self.wayLabel = [[UILabel alloc] init];
//        self.wayLabel = [[UILabel alloc]init];
        [_wayLabel sizeToFit];
        _wayLabel.numberOfLines = 0;
        //        _wayLable.backgroundColor =[UIColor redColor];
        _wayLabel.font = [UIFont fontWithName:@"Georgia" size:12];
        [self.contentView addSubview:_wayLabel];
        [_wayLabel release];
        
        self.coverView = [[UIImageView alloc]init];
        self.coverView.image = [UIImage imageNamed:@"clock.png"];
        self.coverView.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_coverView];
        [_coverView release];
        
        self.timeLabel = [[UILabel alloc]init];
        _timeLabel.font = [UIFont fontWithName:@"Georgia" size:12];
        _timeLabel.textColor = [UIColor colorFromHexCode:@"#79CDCD"];
        [self.contentView addSubview:_timeLabel];
        [_timeLabel release];
        
        
        
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    
}

- (void)setAbout2Waydata:(AboutType_2WayData *)about2Waydata
{
    if (self.about2Waydata != about2Waydata) {
        [_about2Waydata release];
        _about2Waydata = [about2Waydata retain];
    }
    
    [_photoView setImageWithURL:[NSURL URLWithString:[_about2Waydata.photo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    _wayLabel.text = _about2Waydata.text;
  
    _timeLabel.text = [self wayPointTime:_about2Waydata.date_added];
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    NSString *str = self.about2Waydata.text;
    
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:12]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    CGFloat imageHeight =[[self.about2Waydata.photo_info objectForKey:@"h"] floatValue];
    CGFloat imageWidth =[[self.about2Waydata.photo_info objectForKey:@"w"] floatValue];
    if (imageWidth == 0) {
        imageWidth = 1;
    }
    _photoView.frame = CGRectMake(0,0, self.contentView.frame.size.width, self.contentView.frame.size.width * imageHeight / imageWidth);
    _wayLabel.frame = CGRectMake(10,_photoView.frame.size.height + 5, self.contentView.frame.size.width - 5,rect.size.height);
    
    //cell高度 减去 图片 和 文字 的 高度
//    CGFloat subImageAndLableHeight = self.contentView.frame.size.height - _photoView.frame.size.height - _wayLabel.frame.size.height;
    
    //时间lable frame
    _timeLabel.frame = CGRectMake(self.contentView.frame.size.width * 6 / 10, _wayLabel.frame.size.height + _photoView.frame.size.height + 15, self.contentView.frame.size.width * 4 / 10, 20);
    //表图片
    _coverView.frame =CGRectMake(self.contentView.frame.size.width * 1 / 2, _wayLabel.frame.size.height + _photoView.frame.size.height + 15, self.contentView.frame.size.width / 10, 20);
}

#pragma mark 时间戳计算

- (NSString *)wayPointTime : (NSNumber *)numberTime
{
    NSTimeInterval timeTV = [numberTime doubleValue];
    
    //    NSString* timeStr = @"2011-01-26 17:40:50";
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
    //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
    //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    //    NSDate* date = [formatter dateFromString:timeStr]; //------------将字符串按formatter转成nsdate
    //    NSLog(@"data === %@",date);
    
    //    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    //    NSLog(@"datanow === %@",datenow);
    //    NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成nsstring
    
    //    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    //    NSLog(@"timeSp(时间戳):%@",timeSp); //时间戳的值
    //    时间戳转时间的方法
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeTV];
    //    NSLog(@"(国际时区的时间)1415107016  = %@",confromTimesp);
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    //    NSLog(@"(当前的时间)confromTimespStr =  %@",confromTimespStr);
    
    return confromTimespStr;
}



@end
